#include "client_fun.h"

//菜单打印函数
//参数:菜单宏
void printf_choose(int choose)
{
	switch(choose)
	{
		case GUI_LOGIN://登录菜单
			printf("**************************************\n");
			printf("**1.管理员模式 2.普通用户模式 3.退出**\n");
			printf("**************************************\n");
			break;	
		case GUI_ADMIN_LOG://管理员功能菜单
			printf("***************************************\n");
			printf("**1.查询 2.修改 3.添加用户 4.删除用户**\n");
			printf("**     5.查询历史记录      6.退出    **\n");
			printf("***************************************\n");
			break;
		case GUI_USER_LOG://普通用户菜单
			printf("**************************************\n");
			printf("** 1.查询       2.修改       3.退出 **\n");
			printf("**************************************\n");
			break;
		case GUI_ADMIN_SELE://管理员查找功能菜单
			printf("**************************************\n");
			printf("**1.按工号查询   2.查询所有   3.退出**\n");
			printf("**************************************\n");
			break;
		case GUI_ADMIN_UPDATE://管理员更新数据功能菜单
			printf("******************************************\n");
			printf("* 1.住址 2.电话 3.密码 4.姓名 5.职位     *\n");
			printf("* 6.工资 7.入职时间 8.级别 9.年龄 10.退出*\n");
			printf("******************************************\n");
			break;
		case GUI_USER_UPDATE://普通用户更新数据功能菜单
			printf("******************************************\n");
			printf("*   1.住址   2.电话    3.密码   4.退出   *\n");
			printf("******************************************\n");
			break;
		default:
			break;
	}
	print("请输入你的选择(数字)>>");
}
//数据发送函数
//参数:用于通信的套接字,通信结构体指针
int tran_msg_client_fun(int sockfd,TranMsg *tranmsg)
{
	while(1){
		printf_choose(GUI_LOGIN);//打印菜单
		//检查输入-->
		if(scanf("%d",&tranmsg->usertype) != 1){
			while(getchar() != '\n');
			print("请输入你的选择(数字)>>");
		}
		while(getchar() != '\n');
		//检查输入<--
		switch(tranmsg->usertype)
		{
			case admin://管理员登录
				tranmsg->usertype = 0;//设定权限
				if(log_client(sockfd,tranmsg) == USR_LOGIN_SUCCEED){
					printf("管理员模式!\n");
				}else{
					break;
				}
				opera_client(sockfd,tranmsg);//功能选择函数
				break;
			case user://普通用户登录
				tranmsg->usertype = 1;//设定权限
				if(log_client(sockfd,tranmsg) == USR_LOGIN_SUCCEED){
					printf("用户模式!\n");
				}else{
					break;
				}
				opera_client(sockfd,tranmsg);//功能选择函数 
				break;
			case quit:
				printf("感谢使用!\n");
				exit(-1);
			default :
				break;
		}
	}
}

//登录函数
//参数:用于通信的套接字,通信结构体指针
int log_client(int sockfd,TranMsg *tranmsg)
{
	memset(tranmsg->usrinfo.name,0,sizeof(tranmsg->usrinfo.name));//清空
	memset(tranmsg->usrinfo.passwd,0,sizeof(tranmsg->usrinfo.passwd));//清空
	print("请输入用户名>>");
	//获取用户名
	while(1)
	{
		//检查输入-->
		if(read(0,tranmsg->usrinfo.name, sizeof(tranmsg->usrinfo.name)) > 8){
			printf("用户名在8位以内,请重新输入");
			memset(tranmsg->usrinfo.name,0,sizeof(tranmsg->usrinfo.name));
		}else{
			tranmsg->usrinfo.name[strlen(tranmsg->usrinfo.name)-1] = '\0';
			break;
		}
		//检查输入<--
	}
	print("请输入密码>>");
	//获取密码
	while(1)
	{
		//检查输入-->
		if(read(0,tranmsg->usrinfo.passwd, sizeof(tranmsg->usrinfo.passwd)) > 8){
			printf("密码在8位以内,请重新输入");
			memset(tranmsg->usrinfo.passwd,0,sizeof(tranmsg->usrinfo.passwd));
		}else{
			tranmsg->usrinfo.passwd[strlen(tranmsg->usrinfo.passwd)-1] = '\0';
			break;
		}
		//检查输入<--
	}
	tranmsg->cmdtype = log;//写入操作指令
	send(sockfd,tranmsg,sizeof(TranMsg),0);//发送
	//等待回复
	if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
		//判断回复的指令
		if(tranmsg->cmdtype == USR_LOGIN_SUCCEED){
			print("欢迎使用,");
			return USR_LOGIN_SUCCEED;
		}else{
			printf("登录失败,请检查信息!\n");
			return USR_LOGIN_FAILED;
		}
	}
	return 0;
}

//功能选择函数
//参数:用于通信的套接字,通信结构体指针
int opera_client(int sockfd, TranMsg *tranmsg)
{
	while(1){
		//判断权限
		if(tranmsg->usertype == 0){//管理员模式
			//打印菜单
			printf_choose(GUI_ADMIN_LOG);
			//检查输入-->
			if(scanf("%d",&tranmsg->cmdtype) != 1){
				while(getchar() != '\n');
				print("请输入你的选择(数字)>>");
			}
			while(getchar() != '\n');
			//检查输入<--
			tranmsg->cmdtype+=10;
			switch(tranmsg->cmdtype){
				case find://查找功能
					find_client(sockfd,tranmsg);//调用查找函数
					break;
				case update://更新数据功能
					update_client(sockfd,tranmsg);
					break;
				case add://添加用户功能
					add_client(sockfd,tranmsg);
					break;
				case delet://删除用户功能
					delet_client(sockfd,tranmsg);
					break;
				case history://查询历史功能
					history_client(sockfd,tranmsg);
					break;
				case quit_funciton:
					return 0;
				default :
					break;
			}
		}else{//普通用户模式
			printf_choose(GUI_USER_LOG);
			//检查输入-->
			if(scanf("%d",&tranmsg->cmdtype) != 1){
				while(getchar() != '\n');
				print("请输入你的选择(数字)>>");
			}
			while(getchar() != '\n');
			//检查输入<--
			tranmsg->cmdtype+=10;
			switch(tranmsg->cmdtype)
			{
				case find:
					find_client(sockfd,tranmsg);
					break;
				case update:
					update_client(sockfd,tranmsg);
					break;
				case add:
					return 0;
				default:
					break;
			}
		}
	}
	return 0;
}

//查找函数
//参数:用于通信的套接字,通信结构体指针
int find_client(int sockfd,TranMsg *tranmsg)
{
	//判断权限
	if(tranmsg->usertype == 0){
		//打印菜单
		printf_choose(GUI_ADMIN_SELE);
		//检查输入-->
		if(scanf("%d",&tranmsg->cmdtype) != 1){
			while(getchar() != '\n');
			print("请输入你的选择(数字)>>");
		}
		while(getchar() != '\n');
		//检查输入<--
		tranmsg->cmdtype+=20;
		switch(tranmsg->cmdtype)
		{
			case find_id://按工号查找
				tranmsg->cmdtype = find_id;
				print("请输入工号>>");
				//检查输入-->
				if(scanf("%d",&tranmsg->usrinfo.staffno) != 1){
					while(getchar() != '\n');
					print("请输入你的选择(数字)>>");
				}
				while(getchar() != '\n');
				//检查输入<--
				send(sockfd,tranmsg,sizeof(TranMsg),0);
				break;
			case  find_all://查找全部
				tranmsg->cmdtype = find_all;
				send(sockfd,tranmsg,sizeof(TranMsg),0);
				break;
			case quit_find:
				return 0;
			default :
				break;
		}
	}else{
		tranmsg->cmdtype = find;
		send(sockfd,tranmsg,sizeof(TranMsg),0);
	}
	//等待回复
	while(1)
	{
		if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
			//判断回复的指令
			if(tranmsg->cmdtype == FIND_OVER){
				break;
			}else if(tranmsg->cmdtype == FIND_NOT){
				printf("NO FIND THIS USER\n");
				return FIND_NOT;
			}else{
				printf("%s\n",tranmsg->buf);
			}
		}
	}
}

//数据更新函数
//参数:用于通信的套接字,通信结构体指针
int update_client(int sockfd, TranMsg *tranmsg)
{
	memset(tranmsg->buf,0,sizeof(tranmsg->buf));//修复bug:修改时,若前一次的值比此次输入的值长,回残留.解决:清空一下上次的输入
	if(tranmsg->usertype == 0){
		printf_choose(GUI_ADMIN_UPDATE);
		//检查输入-->
		if(scanf("%d",&tranmsg->cmdtype) != 1){
			while(getchar() != '\n');
			print("请输入你的选择(数字)>>");
		}
		while(getchar() != '\n');
		//检查输入<--
		if(tranmsg->cmdtype < 10){
			print("请输入工号>>");
			//检查输入-->
			if(scanf("%d",&tranmsg->usrinfo.staffno) != 1){
				while(getchar() != '\n');
				print("请输入你的选择(数字)>>");
			}
			while(getchar() != '\n');
			//检查输入<--
			print("请输入要修改的值>>");
			while(1)
			{
				//检查输入-->
				if(read(0,tranmsg->buf, sizeof(tranmsg->buf)) > 128){
					printf("128字符以内,请重新输入");
					memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				}else{
					tranmsg->buf[strlen(tranmsg->buf)-1] = '\0';
					break;
				}
				//检查输入<--
			}
			//tranmsg->buf[strlen(tranmsg->buf)-1] = '\0';
		}
		tranmsg->cmdtype+=30;
		switch(tranmsg->cmdtype)
		{
		case update_addr:
			//检查输入-->
			if(strlen(tranmsg->buf) > 32){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			//检查输入<--
			tranmsg->cmdtype = update_addr;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_phone:
			if(strlen(tranmsg->buf) > 12){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_phone;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_passwd:
			if(strlen(tranmsg->buf) > 8){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_passwd;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_name:
			if(strlen(tranmsg->buf) > 8){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_name;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_age:
			if(strlen(tranmsg->buf) > 3){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_age;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_work:
			if(strlen(tranmsg->buf) > 32){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_work;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_date:
			if(strlen(tranmsg->buf) > 32){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_date;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_level:
			if(strlen(tranmsg->buf) > 2){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_level;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_salary:
			if(strlen(tranmsg->buf) > 9){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_salary;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case quit_update:
			return 0;
		default :
			break;
		}
	}else{	
		printf_choose(GUI_USER_UPDATE);
		//检查输入-->
		if(scanf("%d",&tranmsg->cmdtype) != 1){
			while(getchar() != '\n');
			print("请输入你的选择(数字)>>");
		}
		while(getchar() != '\n');
		//检查输入<--
		print("请输入要修改的值>>");
		while(1)
		{
			memset(tranmsg->buf,0,sizeof(tranmsg->buf));
			//检查输入-->
			if(read(0,tranmsg->buf, sizeof(tranmsg->buf)) > 128){
				printf("128字符以内,请重新输入");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
			}else{
				tranmsg->buf[strlen(tranmsg->buf)-1] = '\0';
				printf("tranmsg->buf = %s\n",tranmsg->buf);
				break;
			}
			//检查输入<--
		}
		tranmsg->buf[strlen(tranmsg->buf)-1] = '\0';
		tranmsg->cmdtype+=30;
		switch(tranmsg->cmdtype)
		{
		case update_addr:
			if(strlen(tranmsg->buf) > 32){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_addr;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_phone:
			if(strlen(tranmsg->buf) > 12){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_phone;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_passwd:
			if(strlen(tranmsg->buf) > 8){
				printf("输入有误!\n");
				memset(tranmsg->buf,0,sizeof(tranmsg->buf));
				return 0;
			}
			tranmsg->cmdtype = update_passwd;
			send(sockfd,tranmsg,sizeof(TranMsg),0);
			break;
		case update_name:
			return 0;
		default :
			break;
		}

	}
	//等待回复
	while(1)
	{
		if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
			//判断回复的指令
			if(tranmsg->cmdtype == UPDATE_SUCCEED){
				printf("修改成功\n");
				return 0;
			}else{
				printf("修改失败,检查输入!\n");
				break;
			}
		}
	}
}

//添加用户函数
//参数:用于通信的套接字,通信结构体指针
int add_client(int sockfd,TranMsg *tranmsg)
{
	printf("**********热烈欢迎新员工**********\n");
	print("请输入工号>>");
	//检查输入-->
	if(scanf("%d",&tranmsg->usrinfo.staffno) != 1){
		while(getchar() != '\n');
		print("请输入正确的工号>>");
	}
	while(getchar() != '\n');
	//检查输入<--
	print("工号信息一旦录入无法更改,请确认输入是否正确!(Y/N)");
	if(getchar() == 'Y'){
		while(getchar() != '\n');
	}else{
		while(getchar() != '\n');
		return 0;
	}
	print("请输入用户名");
	while(1)
	{
		//检查输入-->
		memset(tranmsg->buf,0,sizeof(tranmsg->buf));
		if(read(0,tranmsg->buf, sizeof(tranmsg->buf)) > 8){
			printf("用户名在8位以内,请重新输入");
			memset(tranmsg->buf,0,sizeof(tranmsg->buf));
		}else{
			tranmsg->buf[strlen(tranmsg->buf)-1] = '\0';
			break;
		}
		//检查输入<--
	}
	//tranmsg->usrinfo.name[strlen(tranmsg->usrinfo.name)-1] = '\0';
	print("请输入密码");
	while(1)
	{
		//检查输入-->
		memset(tranmsg->usrinfo.passwd,0,sizeof(tranmsg->usrinfo.passwd));
		if(read(0,tranmsg->usrinfo.passwd, sizeof(tranmsg->usrinfo.passwd)) > 8){
			printf("密码在8位以内,请重新输入");
			memset(tranmsg->usrinfo.passwd,0,sizeof(tranmsg->usrinfo.passwd));
		}else{
			tranmsg->usrinfo.passwd[strlen(tranmsg->usrinfo.passwd)-1] = '\0';
			break;
		}
		//检查输入<--
	}
	//tranmsg->usrinfo.passwd[strlen(tranmsg->usrinfo.passwd)-1] = '\0';
	print("是否为管理员(Y/N)");
	if(getchar() == 'Y'){
		tranmsg->usrinfo.usertype = 0;
		while(getchar() != '\n');
	}else{
		tranmsg->usrinfo.usertype = 1;
		while(getchar() != '\n');
	}
	tranmsg->cmdtype = add;
	send(sockfd,tranmsg,sizeof(TranMsg),0);
	//等待回复
	while(1)
	{
		if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
		//判断回复的指令
			if(tranmsg->cmdtype == ADD_SUCCEED){
				printf("添加成功\n");
				return 0;
			}else{
				printf("添加失败,检查输入!\n");
				break;
			}
		}
	}
}

//删除用户函数
//参数:用于通信的套接字,通信结构体指针
int delet_client(int sockfd,TranMsg *tranmsg)
{
	print("请输入要删除的工号>>");
	//检查输入-->
	if(scanf("%d",&tranmsg->usrinfo.staffno) != 1){
		while(getchar() != '\n');
		print("请输入正确的工号>>");
	}
	while(getchar() != '\n');
	//检查输入<--
	tranmsg->cmdtype = delet;
	send(sockfd,tranmsg,sizeof(TranMsg),0);
	//等待回复
	while(1)
	{
		if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
			//判断回复的指令
			if(tranmsg->cmdtype == DELETE_SUCCEED){
				printf("删除成功\n");
				return 0;
			}else{
				printf("删除失败,检查输入!\n");
				break;
			}
		}
	}
}

//历史记录函数
//参数:用于通信的套接字,通信结构体指针
int history_client(int sockfd,TranMsg *tranmsg)
{
	tranmsg->cmdtype = history;
	send(sockfd,tranmsg,sizeof(TranMsg),0);
	//等待回复
	while(1)
	{
		if(recv(sockfd, tranmsg,sizeof(TranMsg), 0)>0){
			//判断回复的指令
			if(tranmsg->cmdtype == HISTORY_OVER){
				return 0;
			}else if(tranmsg->cmdtype == HISTORY_FAILE){
				printf("获取历史记录失败!\n");
				break;
			}else{
				printf("%s\n",tranmsg->buf);
			}
		}
	}

}
